package cn.itcast.www.junitTest;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;

public class DelFile {
	public static void main(String[] args) throws Exception{
		FileInputStream fis = new FileInputStream("D:\\debug.txt");
		Scanner sc = new Scanner(fis, "gbk");
		while (sc.hasNext()) {
			String s = sc.nextLine();
			//System.out.println(s);
			if(s.startsWith("[ERROR] urls")){
				int start = s.indexOf("F:");
				int end = s.lastIndexOf('/');
				String temp = s.substring(start, end);
				//System.out.println(temp);
				Path path = Paths.get(temp);
				testFiles(path);
			}
		}
	}
	
	private static void testFiles(Path dir) throws IOException {
	    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
	        @Override
	        public FileVisitResult preVisitDirectory(Path dir,
	        BasicFileAttributes attrs) throws IOException {
	            System.out.println("目录："+dir);
	            return FileVisitResult.CONTINUE;
	        }
	        @Override
	        public FileVisitResult visitFile(Path file,
	        BasicFileAttributes attrs) throws IOException {
	            System.out.println("文件："+file);
	            Files.delete(file);
	            return FileVisitResult.CONTINUE;
	        }
	        @Override
	        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
	        throws IOException {
	            Files.delete(dir);
	            return FileVisitResult.CONTINUE;
	        }
	        @Override
	        public FileVisitResult visitFileFailed(Path file, IOException exc)
	        throws IOException {
	            return FileVisitResult.CONTINUE;
	        }
	    });
	}
}
